<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/28
 * Time: 17:25
 */

namespace App\Model;


use Illuminate\Database\Eloquent\Model;

class StoresCenter extends Model
{
    /**
     * 门店中心
     * @var string
     */
    protected $table = 'stores_center';
    protected $fillable = [
        'id',
        'uid',
        'shop_name',
        'number',
        'name',
        'phone',
        'province',
        'city',
        'score',
        'type',
        'earnest_money',
        'invoices',
        'resale',
        'replenish_time',
        'district',
        'address',
        'latitude',
        'apply_time',
        'audit_time',
        'updated_at',
        'created_at',
        'earnest_money',
    ];

    /**
     * 添加申请
     * @param array $data
     * @return bool
     */
    public function add($data = array()) {
        if (empty($data)) {
            return false;
        }
        $rico = $this->create($data);
        if ($rico) {
            return $rico;
        } else {
            return false;
        }
    }


    /**
     * 获取全部门店数据
     * @param array $keyword
     * @param bool $pag
     * @return bool|\Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function get_store($keyword = array(),$pag =true) {
        $table = $this;
        //门店状态
        if( !empty( $keyword ) && $keyword['type'] != "" ){
            $table = $this->where( [ 'type' => intval( $keyword['type'] ) ] );
        }
        //关键字
        if(!empty($keyword['keyword'])){
            $table = $this->where('shop_name','like','%'.$keyword['keyword'].'%')
            ->orWhere('number','like','%'.$keyword['keyword'].'%')
            ->orWhere('name','like','%'.$keyword['keyword'].'%')
            ->orWhere('phone','like','%'.$keyword['keyword'].'%');
        }
        if($pag){
            $table = $table->orderBy('apply_time','desc');
            $order['order'] = $table->paginate(15);
        } else {
            $order = $table->orderBy('apply_time','desc')->get()->toArray();
        }
        if ($order) {
            return $order;
        } else {
            return false;
        }
    }


}